#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QFileDialog>
#include <QSettings>
#include <QStandardPaths>
#include <memory>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->label_image->clear();
}

Widget::~Widget()
{
    delete ui;
}
void Widget::open1()
{
    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", "D:/", "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return;
    }

    ui->lineEdit_path->setText(fileName);
    ui->label_image->setPixmap((QPixmap(fileName)));
}

//config就放在对应项目的编译路径下
void Widget::open2()
{
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;
    QSettings *pIniSet = new QSettings(config_path, QSettings::IniFormat);
    QString lastPath = pIniSet->value("/LastPath/path").toString();
    qDebug() << lastPath;

    if(lastPath.isEmpty())
    {
        //如果为空，则把上次的路径设置为图片的路径
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    qDebug() << lastPath;
    QString fileName = QFileDialog::getOpenFileName(this,"请选择图片",lastPath,"图片(*.png *.jpg);;");
    if(fileName.isEmpty())
    {
        return;
    }

    ui->lineEdit_path->setText(fileName);
    ui->label_image->setPixmap((QPixmap(fileName)));

    //保存路径名
    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    pIniSet->setValue("/LastPath/path", _path);
    delete pIniSet;
    pIniSet = nullptr;
    qDebug() << _path;
}

//使用智能指针
void Widget::open3()
{
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;

    //使用智能指针控制内存
    std::unique_ptr<QSettings> pIniSet(new QSettings(config_path, QSettings::IniFormat));
    QString lastPath = pIniSet->value("/LastPath/path").toString();
    qDebug() << lastPath;

    if(lastPath.isEmpty())
    {
        //如果为空，则把上次的路径设置为图片的路径
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    qDebug() << lastPath;
    QString fileName = QFileDialog::getOpenFileName(this,"请选择图片",lastPath,"图片(*.png *.jpg);;");
    if(fileName.isEmpty())
    {
        return;
    }

    ui->lineEdit_path->setText(fileName);
    ui->label_image->setPixmap((QPixmap(fileName)));

    //保存路径名
    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    pIniSet->setValue("/LastPath/path", _path);
    qDebug() << _path;
}

void Widget::open4()
{
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;

    //使用智能指针控制内存
    std::unique_ptr<QSettings> pIniSet(new QSettings(config_path, QSettings::IniFormat));
    QString lastPath = pIniSet->value("/LastPath/path").toString();
    qDebug() << lastPath;

    if(lastPath.isEmpty())
    {
        //如果为空，则把上次的路径设置为图片的路径
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    qDebug() << lastPath;
    QString fileName = QFileDialog::getOpenFileName(this,"请选择图片",lastPath,"图片(*.png *.jpg);;");
    if(fileName.isEmpty())
    {
        return;
    }

    ui->lineEdit_path->setText(fileName);

    QPixmap *pix = new QPixmap(fileName);
    pix->scaled(ui->label_image->size(), Qt::KeepAspectRatio);
    ui->label_image->setScaledContents(true);
    ui->label_image->setPixmap(*pix);

    delete pix;
    pix = nullptr;

    //保存路径名
    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    pIniSet->setValue("/LastPath/path", _path);
    qDebug() << _path;
}
void Widget::on_btnOpen_clicked()
{
    //qDebug() << "click 11111";
    open4();
}


